import 'dart:io';

import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:new_yuqib_app/yqbConfig/log_utils.dart';
import 'package:new_yuqib_app/yqbConfig/storage_config.dart';
import 'package:new_yuqib_app/yqbConfig/yqb_app.dart';
import 'package:new_yuqib_app/yqbConfig/yqb_base.dart';
import 'package:new_yuqib_app/yqbConfig/yqb_toast.dart';
import 'package:new_yuqib_app/yqbNetwork/BaseModel.dart';

import 'yqb_req_api.dart';

class YqbDioRequest {
  static Dio dio = Dio();

  static Future<BaseModel> postRequest(String service,{Map<String, dynamic>? queryParameters}) async {
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client){
      client.findProxy = (url) {
                // return 'PROXY 192.168.55.136:8888';
                return 'PROXY 192.168.31.85:8888';
              };
              client.badCertificateCallback =
                  (X509Certificate cert, Strigbhost, int port) => true; //忽略证书
              return client;
    };

    final headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/x-www-form-urlencoded',
      "appVersion": YqbApp.packageInfo.version,
    };
    dio.options.headers = headers;
    Map<String, dynamic> dataMap = {"app_bundle_label": appBundleLabel, if(queryParameters!=null) ...queryParameters};
    if (StorageConfig.token.isNotEmpty) dataMap["token"] = StorageConfig.token;
    final res = await dio.post(ReqApi.baseUrl + service, data: dataMap);
    var reqData = BaseModel.fromJson(res.data);
    if (reqData.code == 1 || reqData.code == 200) {
      LogUtil.e(reqData.toString());
      return BaseModel.fromJson(res.data);
    } else if (reqData.code == 501) {
      YqbToast.show(reqData.msg);
      throw reqData;
    } else {
      YqbToast.show(reqData.msg);
      throw reqData;
    }
  }
}
